package com.huluobo.a2206douyindemo.adapter;

import android.animation.ObjectAnimator;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;

import androidx.annotation.NonNull;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.huluobo.a2206douyindemo.R;
import com.huluobo.a2206douyindemo.utils.ImageUtils;
import com.huluobo.base.bean.VideoBeanResult;
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer;

/**
 * Created by LC on 2024/8/21.
 */
public class MultiVideoAdapter extends BaseMultiItemQuickAdapter<VideoBeanResult.VideoBean, BaseViewHolder> {
    public MultiVideoAdapter() {
        addItemType(0, R.layout.item_videos);
        addItemType(1, R.layout.item_videos_live);
        addItemType(2, R.layout.item_videos_full);
    }

    @Override
    protected void convert(@NonNull BaseViewHolder baseViewHolder, VideoBeanResult.VideoBean videoBean) {
        //所有布局都有的左下方和背景播放器
        StandardGSYVideoPlayer player = baseViewHolder.getView(R.id.item_videos_gsyPlayer);
        //隐藏播放器返回按钮
        player.getBackButton().setVisibility(View.GONE);
        //播放地址 cacheWithPlay 边播边存
        player.setUp(videoBean.getVideopath(), true, "");

        //左下方区域赋值
        //地址 由于bean类中地址项有空值,所以识别的时候是个object
        baseViewHolder.setText(R.id.item_videos_address_tv, videoBean.getAddress() + "");
        //作者名
        baseViewHolder.setText(R.id.item_videos_authname_tv, videoBean.getAuthname());
        //描述
        baseViewHolder.setText(R.id.item_videos_caption_tv, videoBean.getCaption());
        //跑马灯
        baseViewHolder.setText(R.id.item_videos_pao_tv, videoBean.getAuthname() + "的原创作品");

        //针对全屏布局和不同布局,单独赋值右下方
        if (videoBean.getType() == 0 || videoBean.getType() == 2) {
            //右下方区域赋值
            //头像
            ImageUtils.getInstance().loadCircleImage(getContext(), videoBean.getHeadpath(),
                    baseViewHolder.getView(R.id.item_videos_header_iv));
            //关注状态 0代表未关注,1代表已关注
            if (videoBean.getGuanzhu() == 0) {
                baseViewHolder.getView(R.id.item_videos_add_iv).setVisibility(View.VISIBLE);
            } else {
                baseViewHolder.getView(R.id.item_videos_add_iv).setVisibility(View.GONE);
            }
            //点赞状态 0代表未点赞,1代表已点赞
            if (videoBean.getDianzan() == 0) {
                baseViewHolder.setImageResource(R.id.item_videos_like_iv, R.drawable.ic_xihuan);
            } else {
                baseViewHolder.setImageResource(R.id.item_videos_like_iv, R.drawable.ic_xihuanred);
                Animation likeAnim = AnimationUtils.loadAnimation(getContext(), R.anim.anim_scale);
                baseViewHolder.getView(R.id.item_videos_like_iv).startAnimation(likeAnim);
            }
            //点赞数量
            baseViewHolder.setText(R.id.item_video_like_count_tv, videoBean.getLike_count() < 10000 ?
                    videoBean.getLike_count() + "" : videoBean.getLike_count() / 10000 + "W");
            //评论数量
            baseViewHolder.setText(R.id.item_video_comment_count_tv, "5.5W");
            //收藏数量
            baseViewHolder.setText(R.id.item_video_collect_count_tv, "3.2W");
            //分享数量
            baseViewHolder.setText(R.id.item_video_share_count_tv, "2.2W");
            //音乐播放器,旋转动画
            ImageView musicPlayer = baseViewHolder.getView(R.id.item_videos_music_player_iv);
            ImageUtils.getInstance().loadCircleImage(getContext(), videoBean.getVideomainimg(), musicPlayer);

            ObjectAnimator animator = ObjectAnimator.ofFloat(musicPlayer, "Rotation", 0, 360);
            animator.setDuration(3000);
            animator.setRepeatCount(-1);
            animator.setInterpolator(new LinearInterpolator());
            animator.start();
        }
    }
}
